<?php
/**
 * text
 *
 * script
 *
 * @version:	0.1a
 * @package:	global
 * @author:	alex_ez
 * @created:	27.01.2009 01:52:13
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */


class newsAdminController extends adminController
{
	static public function getMenuItems( )
	{
		return json::decode( '{"news":{text:"Новости","group":"Контент","order":"-1"}}' );
	}
	
	public function onstart( )
	{
		$this ->visitor = visitor::__getMe( );
		$this ->visitor ->needAuth( 'admin' );
		$this ->frame = 'admin/!index.tpl';
		$this ->model = new news( );
	}
	
	public function showIndexPage( $p, $o )
	{
		$r = array( );
		$r['list'] = $this ->model ->getBy( array( ), 'date-,tm-' );
		$r['title'] = 'Новости';
		
		return $r;
	}

	public function showAddPage( $p, $o )
	{
		$form = new Form( $this, 'admin/news/add' );
		$form ->setOption( 'name', 'addnews' );
		$form ->setOption( 'action', '/ajax/admin/news/add/' );
		
		$r['fields'] = $form ->getFieldSet( );
		$r['options'] = $form ->getOptions( );
		$r['template'] = 'admin/__form.tpl';
		$r['title'] = 'Добавление новости';
		
		//fb::log( $r ); //"{'action':'/ajax/info/cashless/add/','method':'POST','name':'addoffer'}";
		return $r;
	}
	
	public function ajaxAdd( $z )
	{
		$form = new Form( $this, 'admin/news/add' );
		// fetch post data & if data is bogus return error
		try { $p = $form ->getPostData( ); } catch( FormException $e ) { return array( 'error' => $e ->getMessage() ); }
		
		$p['date'] = date( 'Y-m-d H:i:s', $p['date'] );
		
		$id = $this ->model ->add( $p );
		if( $id<=0 ) return array( 'error' => 'Новость не добавлена. Внутренняя ошибка.' );
		
		return array( 'message' => 'Новость добавлена. id: '.$id, 'location' => '/admin/news/' );
	}
	
	//new textModel( );
	
	public function showEditPage( $p, $o )
	{
		$id = (int)(Request::__getMe( ) ->g_id);
		$d = $this ->model ->get( $id );
		if( !$d ) Response::__getMe( ) ->notfound( );
		
		$form = new Form( $this, 'admin/news/add' );
		$form ->setOption( 'name', 'editnews' );
		$form ->setOption( 'action', '/ajax/admin/news/edit/?id='.$d['id'] );
		$d['date'] = date('d.m.Y',strtotime($d['date']));
		$form ->setData( $d );
		
		$r = array( );
		$r['fields'] = $form ->getFieldSet( );
		$r['options'] = $form ->getOptions( );
		$r['template'] = 'admin/__add.tpl';
		$r['title'] = "Редактирование новости";
		
		return $r;
	}
	
	
	public function ajaxEdit( $z )
	{
		$form = new Form( $this, 'admin/news/add' );
		// fetch post data if data is bogus return error
		try { $p = $form ->getPostData( ); } catch( FormException $e ) { return array( 'error' => $e ->getMessage() ); }
		
		$id = (int)( Request::__getMe( ) ->g_id );
		if( $id<=0 ) return array( 'error' => 'Новость не существует' );
		
		$p['date'] = date( 'Y-m-d H:i:s', $p['date'] );
		
		$eo = $this ->model ->edit( $id, $p );
		if( $eo!=1 ) return array( 'error' => 'Новость не отредактировалась' );
		
		return array( 'message' => 'Новость отредактирована', 'location' => '/admin/news/' );
	}
	
	public function showRemovePage( $p, $o )
	{
		$this ->pageTitle = 'remove';
		
		$id = (int)(Request::__getMe( ) ->g_id);
		
		$this ->model ->remove( $id );
		
		Response::__getMe( ) ->moved( '/admin/news/' );
	}
	
	
	
	
	/**
	 * Fields
	 *
	 */
	public function getFieldSet( $url = null, $decode = false )
	{
		$iurl = is_null( $url ) ? $this ->getInnerUrl( ) : $url;
		$r = isset( $this ->fieldSets[ $iurl ] ) ? $this ->fieldSets[ $iurl ] : ''; //json::decode( $fields );
		
		$replacements = array(
			'now' => date('d.m.Y'),
			'yesterday' => date('d.m.Y',time()-86400),
			'tomorrow' => date('d.m.Y',time()+86400),
			'user.login' => visitor::__getMe() ->user['login'],
			);
		
		// replacing
		$r = str_replace(
			array_map( create_function('$v','return "{{$v}}";'), array_keys( $replacements ) ),
			array_values( $replacements ),
			$r );
		
		return $decode ? json::decode( $r ) : $r;
	}

	
	private $fieldSets = array(
'admin/news/add' =>
"{'head':	{type:'header',	label:'Новость'},
'date':		{type:'date',	label:'Дата',value:'__.__.____',required:true},
'title':	{type:'string',	label:'Заголовок',required:true},
'short':	{type:'text',	label:'Краткое содержание',required:true},
'body':		{type:'text',	label:'Полное содержание',required:true}
}",
	);
}

?>